package com.example.sys.attachment.quartz;

import com.example.sys.attachment.po.SysAttachment;
import com.example.sys.attachment.service.SysAttachmentService;
import com.example.sys.job.annotation.JobScope;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/***
 * 每晚1点执行清理垃圾文件的操作、
 */
@JobScope(name = "附件清理任务", cron = "0 0 1 * * ?", desc = "清理无效的垃圾附件信息")
@Service
public class SysAttachmentCleanJob implements Job {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysAttachmentService sysAttachmentService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<SysAttachment> clearList = this.sysAttachmentService.findClearList();
        for (SysAttachment sysAttachment : clearList) {
            try{
                this.sysAttachmentService.getSysFileService().delete(sysAttachment.getPath());
                this.sysAttachmentService.removeById(sysAttachment.getId());
            }catch (Exception e){
                logger.warn("附件清理任务清理出错: " + sysAttachment.getId());
            }
        }
    }
}
